package sfsoftware.game.actor.enemy.boss;

import sfsoftware.game.actor.enemy.SmartBullet;
import sfsoftware.game.trevor.Boss;
import sfsoftware.game.trevor.Player;
import sfsoftware.util.Clock;
import sfsoftware.util.Sound;
import sfsoftware.util.Tile;

public class Angler extends Boss {
	private Clock fireClock = new Clock(1200);
	
	public Angler(int destX, int destY, String texture, int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, destWidth, destHeight);
		tileMap = assocTileMap;
		setSpeed(2);
		weight = 1.85f;
		Sound.playMusic("angler", true);
	}

	public Angler(int destX, int destY, String texture, int srcX, int srcY,	int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight);
		tileMap = assocTileMap;
		setSpeed(2);
		weight = 1.85f;
		Sound.playMusic("angler", true);
	}
	
	public boolean move(int dirX, int dirY, Tile[][] tileMap)
	{		
		boolean successfulMove = false;
		try
		{
			// Try to move!
			successfulMove = super.move(dirX, dirY, tileMap); 
			if(!successfulMove)
			{				
				// If the move was unsuccessful, change direction
				direction = changeDirection();
			}
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			// If the actor tried to move off the playing field, change direction!
			direction = changeDirection();
		}
		return successfulMove;
	}	
	
	private int changeDirection()
	{
		// If they are going up or down, they change direction to left or right
		if(direction == UP_DIRECTION  ||  direction == DOWN_DIRECTION)
		{
			 return (int)(Math.random() * 2) + 2;
		}
		// Otherwise, they change direction to up or down... 
		// ...makes them seem smarter than they are ;)
		return  (int)(Math.random() * 2);	
	}	

	private double getAngleToPlayer()
	{
		double dx = (getX() + (getWidth() / 2)) - (Player.getXPos() + 20);
		double dy = (getY() + (getHeight() / 2)) - (Player.getYPos() + 20);
		double inRads = Math.atan2(dy,dx);
		
		if (inRads < 0)
		{
	        inRads = Math.abs(inRads);
		}
	    else
	    {
	        inRads = 2 * Math.PI - inRads;
	    }			
		
		return inRads;
	}	
	
	public void update()
	{
		if(fireClock.updateClock())
		{
			new SmartBullet((int)getX() + (width / 2) - 4, (int) getY() + (height / 2) - 4, "orb", 8, 8, tileMap, 10, getAngleToPlayer());
			new SmartBullet((int)getX() + (width / 2) - 4, (int) getY() + (height / 2) - 4, "orb", 8, 8, tileMap, 10, getAngleToPlayer() - 0.2);
			new SmartBullet((int)getX() + (width / 2) - 4, (int) getY() + (height / 2) - 4, "orb", 8, 8, tileMap, 10, getAngleToPlayer() + 0.2);
			Sound.playSound("shooter");
		}
		
		switch(direction)
		{
			case 0:
				this.move(0, 1, tileMap);
				break;
			case 1:
				this.move(0, -1, tileMap);
				break;
			case 2:				
				this.move(1, 0, tileMap);
				break;
			case 3:
				this.move(-1, 0, tileMap);
				break;				
		}
	}
}
